﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKArchBaseCode.OutWallSearch;

namespace YJKRevitDimensionText.VerticalAutoDim.OutWallSearch
{
	internal class Pole2DOutWallCreator : IOutWallCreator
	{
		private IDictionary<ElementId, IList<OutWallData>> MapLevelId2OutWallData { get; set; }

		public IDictionary<ElementId, IList<OutWallData>> LevelId2OutWallData
		{
			get
			{
				return this.MapLevelId2OutWallData;
			}
		}

		public Func<Element, bool> IsParapetWall
		{
			get
			{
				return new Func<Element, bool>(Utility.IsValidParapetWall);
			}
		}

		public Pole2DOutWallCreator(Autodesk.Revit.DB.Document doc, IList<ViewPlan> arrayViewPlan)
		{
			this.Doc = doc;
			this.ArrayViewPlan = arrayViewPlan;
			this.MapLevelId2OutWallData = new Dictionary<ElementId, IList<OutWallData>>();
		}

		public bool OutWallSearcher()
		{
			XYZ ViewOrientation = this.Doc.ActiveView.ViewDirection;
			EnumerableExternsion.ForEach<ViewPlan>(this.ArrayViewPlan, delegate(ViewPlan view)
			{
				List<WallData> list = new List<WallData>();
				if (OutWallFilter.Search(this.Doc, view.Id, ref list, true, true))
				{
					List<OutWallData> lstOutWallData = new List<OutWallData>();
					this.MapLevelId2OutWallData.Add(view.GenLevel.Id, lstOutWallData);
					list.ForEach(delegate(WallData data)
					{
						XYZ zero = XYZ.Zero;
						if (data.GetOrientationOuterWall(ref zero) && !data.CenterLine.IsArc)
						{
							Wall wall = data.Doc.GetElement(data.Id) as Wall;
							double width = wall.WallType.Width;
							Curve curve = (wall.Location as LocationCurve).Curve;
							XYZ xyz = curve.GetEndPoint(0) + ViewOrientation * width * 0.5;
							XYZ xyz2 = curve.GetEndPoint(1) + ViewOrientation * width * 0.5;
							Line curve2 = Line.CreateBound(xyz, xyz2);
							lstOutWallData.Add(new OutWallData
							{
								Element = wall,
								Curve = curve2,
								Orientation = zero
							});
						}
					});
				}
			});
			return true;
		}

		private readonly Document Doc;

		private readonly IList<ViewPlan> ArrayViewPlan;
	}
}
